package com.test.springboot.springboot3securitydemo1.controller;

import com.google.code.kaptcha.Producer;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/9/30-11:52
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Controller
public class VerifyCodeController {
  @Autowired private Producer producer;

  @GetMapping("/vc.jpg")
  public void getVerifyCode(HttpServletResponse response, HttpSession session) throws IOException {
    response.setContentType("image/jpeg");
    final String text = producer.createText();
    session.setAttribute("verifyCode", text);
    final BufferedImage image = producer.createImage(text);
    try (final ServletOutputStream out = response.getOutputStream()) {
      ImageIO.write(image, "jpg", out);
    }
  }
}
